package jfupdater.utils;

import java.util.Collections;
import java.util.List;

import jfupdater.service.UpdateInfo;
import jfupdater.ui.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;

public class Preferences {
	
	public static final int UPDATE_FREQ_AT_BOOT = -1;
	public static final int UPDATE_FREQ_EVERY_HOUR = 3600;
	public static final int UPDATE_FREQ_EVERY_DAY =  86400;
	public static final int UPDATE_FREQ_EVERY_WEEK = 604800;
	
	private static List<UpdateInfo> mUpdateList;
	private static UpdateInfo mDownloadedUpdate;
	
	private static Preferences INSTANCE;
	
	private final SharedPreferences mPrefs;
	private final Resources mRes;
	
	private Preferences(SharedPreferences prefs, Resources res) {
		mPrefs = prefs;
		mRes = res;
	}
	
	public static synchronized Preferences getPreferences(Context ctx) {
		if(INSTANCE == null) {
			INSTANCE = new Preferences(PreferenceManager.getDefaultSharedPreferences(ctx), ctx.getResources());
		}
		
		return INSTANCE;
	}

	public int getUpdateFrequency() {
		try {
			return Integer.parseInt(mPrefs.getString(mRes.getString(R.string.p_update_check_freq), ""));
		} catch (NumberFormatException ex) {
			return UPDATE_FREQ_EVERY_WEEK;
		}
	}
	
	public void setUpdateList(List<UpdateInfo> updateList) {
		mUpdateList = Collections.unmodifiableList(updateList);
	}
	
	public List<UpdateInfo> getUpdateList() {
		return mUpdateList;
	}
	
	public void setDownloadedUpdate(UpdateInfo updateInfo) {
		mDownloadedUpdate = updateInfo;
	}
	
	public UpdateInfo getDownloadedUpdate() {
		return mDownloadedUpdate;
	}
}
